const jwt = require('jsonwebtoken')
const key = 'atguigu_key'

// 加密生成token
const token = jwt.sign({data: '123'}, key, {expiresIn: 3})
// jwt.sign({data: '123'}, key, {expiresIn: '7d'})

setTimeout(() => {
  // 校验token
  jwt.verify(token, key, (error, decoded) => {
    if (error) {
      console.log(error.name)
      if (error.name==='TokenExpiredError') {
        console.log('token过期')
      } else {
        console.log('token非法')
      }
    } else {
      console.log('用户id', decoded.data)
    }
  })
}, 1000);